package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.PlanBatchnoMapper;
import com.indusfo.spc.pojo.Plan;
import com.indusfo.spc.pojo.PlanBatchno;
import com.indusfo.spc.service.BarCodeRules;
import com.indusfo.spc.service.PlanBatchnoService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlanBatchnoServiceImpl implements PlanBatchnoService {

    private  static  final Logger logger = LoggerFactory.getLogger(PlanBatchnoServiceImpl.class);

    @Autowired
    private PlanBatchnoMapper planBatchnoMapper;

    @Autowired
    private BarCodeRules barCodeRules;

    @Override
    public JSONObject insertPlanBatchno(PlanBatchno planBatchno , Integer num) {
        List<String> list = new ArrayList<>();
        if(planBatchno.getPlanId()==null){
            throw new ModifyFailedException("请任务单号");
        }
        if(num == null || num==0){
            throw new ModifyFailedException("请填写数量");
        }
        Integer ruleId = planBatchnoMapper.queryRuleId(planBatchno.getPlanId());
        list = barCodeRules.calculate(ruleId ,num);
        int row =planBatchnoMapper.insertSelective(planBatchno , list);
        if(row == 0) {
            throw new ModifyFailedException("数据新增失败！");
        }
        return JSONObject.oK("新增成功！");
    }

    @Override
    public JSONObject selectPlanBatchno(PlanBatchno planBatchno) {
        List<PlanBatchno> planBatchnoList = null;
        try {
            Integer pagesize = planBatchno.getPagesize();
            Integer pageindex = planBatchno.getPageindex();
            if(pagesize != null && pageindex != null) {
                planBatchno.setIncept(pagesize*(pageindex - 1));
            }
            if(planBatchno.getPlanId()== null){
                throw new ParamsErrorException("请选择任务单号");
            }
            //查询全部
            planBatchnoList = planBatchnoMapper.selectPlanBatchno(planBatchno);
            if (planBatchnoList.isEmpty()) {
                return JSONObject.oK("没有查询到数据", planBatchnoList, 0);
            }
            //查询分页总记录数
            int count = planBatchnoMapper.countPlanBatchno(planBatchno); //返回查询到的总记录数
            return JSONObject.oK("查询成功", planBatchnoList, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject delPlanBatchno(Long[] planBatchnoIds) {
        if (planBatchnoIds == null || planBatchnoIds.length==0) {
            throw new ParamsErrorException("请选择批号");
        }
        String msg = "";
        int row = planBatchnoMapper.delPlanBatchno(planBatchnoIds);
        if(row == 0){
            msg = "删除失败";
        }else {
            msg = "删除成功";
        }
        return JSONObject.oK(msg);
    }

    @Override
    public JSONObject printPlanBatchno(Long[] planBatchnoIds) {
        if (planBatchnoIds == null || planBatchnoIds.length==0) {
            throw new ParamsErrorException("请选择批号");
        }
        String msg = "";
        int row = planBatchnoMapper.printPlanBatchno(planBatchnoIds);
        if(row == 0){
            msg = "打印失败";
        }else {
            msg = "打印成功";
        }
        return JSONObject.oK(msg);
    }

    @Override
    public JSONObject numPlanBatchno(PlanBatchno planBatchno) {
        if (planBatchno.getPlanId() == null || planBatchno.getPlanId()==0) {
            throw new ParamsErrorException("请选择生产任务单号");
        }
        PlanBatchno planBatchnos = planBatchnoMapper.selectPlanBatchnoNum(planBatchno);
        return JSONObject.oK(planBatchnos);
    }

    @Override
    public JSONObject queryPlan(Plan plan) {
        if (plan.getlPlan() == null || plan.getlPlan() == "") {
            throw new ParamsErrorException("请输入生产任务单号");
        }
        Plan plans = planBatchnoMapper.queryPlan(plan);
        if(plans == null){
            return new JSONObject(200,"没有查询到数据", plans);
        }
        return new JSONObject(200,"查询成功", plans);
    }

}
